package com.course.controller;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import com.course.db.ChooseCourse;
import com.course.db.CourseBasic;
import com.course.db.Student;
import com.course.db.Teacher;
import com.course.db.User;
import com.course.safe.MD5;
import com.et.mvc.JspView;

public class StudentController extends ApplicationController {
	public JspView select() throws Exception{
		System.out.println("select course");
		
		int user_id = (Integer)session.getAttribute("userID");
		int courseId = Integer.parseInt(request.getParameter("courseId"));
		
		//get student_id
		int student_id = 0;
 	   	Student student = new Student();
 	   	student.setUser_id( user_id);
 	   	student_id = student.student_id();
 	   	student.setId(student_id);
 	   	student.init();
		
		//check if has checked before
		ChooseCourse choose_course = new ChooseCourse();
		if( !choose_course.getCourse(courseId,student_id) )
		{
			//check grade
			if(!choose_course.check_grade(courseId).contains(student.getGrade().substring(0, 2)))
			{
				System.out.println("年级："+student.getGrade().substring(0, 1));
				response.getWriter().append("该课程不对"+student.getGrade().substring(0, 2)+"开放，请重新选择！");
				return null;
			}
			
			//check num of selected course
			if(choose_course.course_count(student_id)>2)
			{
				response.getWriter().append("选课失败，您选课数目已超过3门！");
				return null;
			}
			
			//check num of students
			if(!choose_course.choose_num_student(courseId))
			{
				response.getWriter().append("选课失败，选择该门课程人数超过限制！");
				return null;
			}
			
			//update course info
			CourseBasic course = new CourseBasic();
			course.setId(courseId);
			course.init();
			course.setNum_of_students(1+course.getNum_of_students());
			course.modify_coursebasic();
			
			//add choose_course
			choose_course.setCourse_id(courseId);
			choose_course.setStudent_id(student_id);
			choose_course.add();
			
			//response.sendRedirect("../Student/mySelect.do");
		}
		else
		{
			//selected, prompt info 
			response.getWriter().append("您已选择该类型课程，请重新选择！");
			//response.sendRedirect("../Search/courseList.do");
		}
		return null;
	}
	
	public JspView unselect() throws Exception{
		System.out.println("unselect");

		int user_id = (Integer)session.getAttribute("userID");
		int courseId = Integer.parseInt(request.getParameter("courseId"));
		
		//get student_id
		int student_id = 0;
 	   	Student student = new Student();
 	   	student.setUser_id( user_id);
 	   	student_id = student.student_id();
		
		ChooseCourse choose_course = new ChooseCourse();
		choose_course.getChoosedCourse(courseId,student_id);
		
		//can not unselect course if it has been checked
		if(choose_course.getStatus() == 1 || choose_course.getStatus() == 2 )
		{
			//prompt info.....
			return null;
		}
		choose_course.delete(choose_course.getId());
		
		//update course
		CourseBasic course = new CourseBasic();
		course.setId(courseId);
		course.init();
		course.setNum_of_students(course.getNum_of_students()-1);
		course.modify_coursebasic();
		//response.sendRedirect("../Student/mySelect.do");
		
		return null;
	}

	public JspView mySelect() throws Exception{
		System.out.println("my select");
		int user_id = (Integer)session.getAttribute("userID");
		
		//get student_id
		int student_id = 0;
 	   	Student student = new Student();
 	   	student.setUser_id( user_id);
 	   	student_id = student.student_id();
 	   	
 	   	ChooseCourse choose_course = new ChooseCourse();
 	   	
 	   	ResultSet chooseRs = choose_course.show_choose_course(student_id);
 	   	
 	   List< Hashtable<String, String> > SelectList = new ArrayList< Hashtable<String, String> >();
		try{
			String value="";
			ResultSetMetaData courseRsMd = chooseRs.getMetaData();
			int cols = courseRsMd.getColumnCount();
			while (chooseRs.next())
			{
				Hashtable<String, String> hash = new Hashtable<String, String>();
				for (int i = 1; i <= cols; i++) {	
					String field = (String) (courseRsMd.getColumnName(i));
					int fieldtype = courseRsMd.getColumnType(i);
					if(fieldtype == java.sql.Types.VARCHAR )
						value = (String) (chooseRs.getString(i));
					else if(fieldtype == java.sql.Types.DOUBLE)
						value = (String)(Double.toString(chooseRs.getDouble(i)));
					else if(fieldtype == java.sql.Types.INTEGER)
						value = (String)(Integer.toString(chooseRs.getInt(i)));
					else if(fieldtype == java.sql.Types.BIGINT)
						value = (String)(Integer.toString(chooseRs.getInt(i)));
					else if(fieldtype == java.sql.Types.SMALLINT)
						value = (String)(Integer.toString(chooseRs.getInt(i)));
					else if(fieldtype == java.sql.Types.TINYINT)
						value = (String)(Integer.toString(chooseRs.getInt(i)));
					else if(fieldtype == java.sql.Types.CHAR)
						value = (String) (chooseRs.getString(i));
					else if(fieldtype == java.sql.Types.DATE)
						value = (String) (chooseRs.getDate(i).toString());
					
					if (value == null)
						value = "";
					hash.put(field, value);
				}
				SelectList.add(hash);
			}
			
			session.setAttribute("SelectList", SelectList);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		response.sendRedirect("../student/myCourse.jspx");
		
		return null;
	}
	
public String edit() throws Exception{
		
		System.out.println("student edit");
		int studentId = Integer.parseInt( request.getParameter("studentId").trim());
		Student student =new Student();
		student.setId(studentId);
		student.init();
		student.setName(request.getParameter("name"));
		student.setGrade(request.getParameter("grade"));
		student.setNo(request.getParameter("studentno"));
		
		student.modify_student();
		
		return "<script type=\"text/javascript\">function closebox(){top.location.href=\"../Admin/userList.do?tab=1\" ; this.parent.Popup.prototype.reset_m();}</script><link rel=\"stylesheet\" href=\"/css/style.css\" />修改成功！<a href=\"javascript:closebox();\">关闭</a>";	
	}
	
public JspView editPwd() throws Exception{
		
		System.out.println("student edit password");
		int user_id = (Integer)session.getAttribute("userID");
		String prepwd = request.getParameter("prepwd");
		String password = request.getParameter("password");
		String mkpwd = request.getParameter("mkpwd");
		
		
		User user = new User();
		user.setId(user_id);
		user.init();
		MD5 oMD5 = new MD5();
		
		if(user.getPassword().equals(oMD5.getMD5ofStr(prepwd)))
		{
			user.setPassword(oMD5.getMD5ofStr(password));
			user.modify_user();
			response.getWriter().append("success");
		}
		else
		{
			response.getWriter().append("false");
		}
	
		
		return null;
	}
}
